#include "startwidget.h"
#include "ui_startwidget.h"
#include <QDebug>
#include <QSettings>
#include <QFileDialog>

#define SETTINGS_FILE_PATH "start_settings.ini"

StartWidget::StartWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::StartWidget)
{
    ui->setupUi(this);

    QString userName;   //用户名
    QString passWord;   //密码
    QString keyWord;    //关键字
    QString saveingPath;//日志保存路径
    int rebootTimes;    //测试次数

    settingsFilePath = (QString("%1/%2").arg(QApplication::applicationDirPath()).arg(SETTINGS_FILE_PATH));
    if(QFile::exists(settingsFilePath)){
        QSettings settings(settingsFilePath, QSettings::IniFormat);
        settings.beginGroup("STARTSettings");
        userName = settings.value("USERNAME").toString();
        passWord = settings.value("PASSWORD").toString();
        keyWord = settings.value("KEYWORD").toString();
        saveingPath = settings.value("SAVINGPATH").toString();
        rebootTimes = settings.value("REBOOTTIMES").toInt();
        settings.endGroup();
    }
    else{
        userName = "kylin705";
        passWord = "A1234567a";
        keyWord = "NULLKEYWORD";
        saveingPath = QApplication::applicationDirPath();
        rebootTimes = 500;
    }

    this->ui->lineEdit_username->setText(userName);
    this->ui->lineEdit_password->setText(passWord);
    this->ui->lineEdit_keyword->setText(keyWord);
    this->ui->lineEdit_savingpath->setText(saveingPath);
    this->ui->lineEdit_reboottimes->setText(QString::number(rebootTimes));
    useCamera = (ui->checkBox_cameraEnable->isChecked()?true:false);

    camera = new Camera;
    emit on_checkBox_cameraEnable_clicked();
}

StartWidget::~StartWidget()
{
    delete camera;
    delete ui;
}

QStringList StartWidget::get_strWaitFor1()
{
    QStringList strWaitFor1;
    strWaitFor1 << "data bist fail" \
                << "Synchronous Abort" \
                << "BIST Failed" \
                << this->ui->lineEdit_keyword->text() \
                << "login:" \
                << "秒后进入系统" \
                << "An error was detected in the test on reboot" \
                << "No DIMM";
    return strWaitFor1;
}

QStringList StartWidget::get_strWaitFor2()
{
    QStringList strWaitFor2;
    strWaitFor2 << this->ui->lineEdit_keyword->text() \
                << "login:" \
                << "Password: " \
                << ":c~$";
    return strWaitFor2;
}

QString StartWidget::get_savingPath()
{
    return this->ui->lineEdit_savingpath->text();
}

QString StartWidget::get_userName()
{
    return this->ui->lineEdit_username->text();
}

QString StartWidget::get_passWord()
{
    return this->ui->lineEdit_password->text();
}

QString StartWidget::get_keyWord()
{
    return this->ui->lineEdit_keyword->text();
}

int StartWidget::get_rebootTimes()
{
    return this->ui->lineEdit_reboottimes->text().toInt();
}

void StartWidget::on_pushButton_begin_clicked()
{
    qDebug() << "Reboot Begin";

    this->ui->lineEdit_username->setEnabled(false);
    this->ui->lineEdit_password->setEnabled(false);
    this->ui->lineEdit_keyword->setEnabled(false);
    this->ui->lineEdit_reboottimes->setEnabled(false);
    this->ui->pushButton_begin->setEnabled(false);
    this->ui->pushButton_end->setEnabled(true);
    this->ui->lineEdit_savingpath->setEnabled(false);

    RemainRebootTimes = this->ui->lineEdit_reboottimes->text().toInt();

    emit signalBeginReboot();
}

void StartWidget::on_pushButton_end_clicked()
{
    qDebug() << "Reboot End";

    ui->lineEdit_username->setEnabled(true);
    ui->lineEdit_password->setEnabled(true);
    ui->lineEdit_keyword->setEnabled(true);
    ui->lineEdit_reboottimes->setEnabled(true);
    ui->pushButton_begin->setEnabled(true);
    ui->pushButton_end->setEnabled(false);
    ui->lineEdit_savingpath->setEnabled(true);

    emit signalEndReboot();
}

void StartWidget::slot_setRemainNum()
{
    ui->lineEdit_remain->setText(QString::number(RemainRebootTimes));
}

void StartWidget::on_checkBox_cameraEnable_clicked()
{
    if(ui->checkBox_cameraEnable->isChecked())
    {
        useCamera = true;
        //ui->pushButton_cameraSetting->setEnabled(true);
    }else
    {
        useCamera = false;
        //ui->pushButton_cameraSetting->setEnabled(false);
    }
}

void StartWidget::on_pushButton_cameraSetting_clicked()
{
    camera->show();
}

void StartWidget::on_pushButton_save_clicked()
{
    QSettings settings(settingsFilePath, QSettings::IniFormat);
    settings.beginGroup("STARTSettings");
    settings.setValue("USERNAME", this->ui->lineEdit_username->text());
    settings.setValue("PASSWORD", this->ui->lineEdit_password->text());
    settings.setValue("KEYWORD", this->ui->lineEdit_keyword->text());
    settings.setValue("SAVINGPATH", this->ui->lineEdit_savingpath->text());
    settings.setValue("REBOOTTIMES", this->ui->lineEdit_reboottimes->text().toInt());
    settings.endGroup();
    qDebug() << "Saving to " << settingsFilePath;
}

void StartWidget::on_pushButton_clicked()
{
    QString directory = QFileDialog::getExistingDirectory(this, tr("Select Directory Please", ""));
    if(!directory.isEmpty()){
        ui->lineEdit_savingpath->setText(directory);
    }
}
